
package com.zcp.notepad;

import android.content.Context;
import android.text.Layout;
import android.text.Selection;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class MyLinkEditText extends EditText {

    private String tel = null;

    private String email = null;

    private String web = null;

    private NoteEditorActivity activity;

    public MyLinkEditText(Context context) {
        super(context);
        initialize(context);
    }

    public MyLinkEditText(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        initialize(paramContext);
    }

    public MyLinkEditText(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        initialize(paramContext);
    }

    private void initialize(Context paramContext) {
        tel = paramContext.getString(R.string.tel);
        email = paramContext.getString(R.string.send_email);
        web = paramContext.getString(R.string.web);

    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int before, int after) {
        if (activity == null)
            return;
        if (text.length() <= 0) {
            activity.disableDoneButton();
        } else {
            activity.enableDoneButton();
        }
        super.onTextChanged(text, start, before, after);
    }

    @Override
    protected void onCreateContextMenu(ContextMenu paramContextMenu) {
        URLSpan[] arrayOfURLSpan = null;
        String str = null;
        String menuName = web;
        if (getText() instanceof Spanned) {
            int startIndex = getSelectionStart();
            int endIndex = getSelectionEnd();
            int min = Math.min(startIndex, endIndex);
            int max = Math.max(startIndex, endIndex);
            arrayOfURLSpan = (URLSpan[]) getText().getSpans(min, max, URLSpan.class);

        }

        if (arrayOfURLSpan != null) {
            MenuItem localMenuItem = null;
            for (int i = 0; i < arrayOfURLSpan.length; i++) {
                str = arrayOfURLSpan[i].getURL();
                menuName = web;
                if (str.indexOf("mailto") >= 0) {
                    menuName = email;
                } else if (str.indexOf("tel") >= 0) {
                    menuName = tel;
                }
                localMenuItem = paramContextMenu.add(0, i, 0, menuName);
                localMenuItem.setOnMenuItemClickListener(new MyOnMenuItemClickListener(
                        arrayOfURLSpan[i], this));

            }

        }

        super.onCreateContextMenu(paramContextMenu);
    }

    public void setActivity(NoteEditorActivity activity) {
        this.activity = activity;
    }

    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        int locationX = (int) paramMotionEvent.getX();
        int locationY = (int) paramMotionEvent.getY();
        int padLeft = getTotalPaddingLeft();
        int charStart = locationX - padLeft;
        int padTop = getTotalPaddingTop();
        int charYStart = locationY - padTop;
        int realX = charStart + getScrollX();
        int realY = charYStart + getScrollY();
        Layout localLayout = getLayout();
        // 获取行数
        int lineY = localLayout.getLineForVertical(realY);
        float f = realX;
        int i8 = localLayout.getOffsetForHorizontal(lineY, f);
        int length = ((ClickableSpan[]) getText().getSpans(i8, i8, ClickableSpan.class)).length;
        if (length > 0) {
            Selection.setSelection(getText(), i8);
        }
        return super.onTouchEvent(paramMotionEvent);

    }

    class MyOnMenuItemClickListener implements MenuItem.OnMenuItemClickListener {
        private URLSpan MyURLSpan = null;

        private View myView;

        public MyOnMenuItemClickListener(URLSpan myURLSpan, View myView) {
            MyURLSpan = myURLSpan;
            this.myView = myView;
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (MyURLSpan != null) {
                MyURLSpan.onClick(myView);
            }
            return false;
        }

    }
}
